package com.lizk.imgbed;


import com.lizk.imgbed.common.enums.NotifyType;
import com.lizk.imgbed.go.GoMethod;
import com.lizk.imgbed.utils.UIUtils;
import com.sun.javafx.stage.StageHelper;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.Pane;
import javafx.stage.FileChooser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {

    private static final Logger logger = LoggerFactory.getLogger(Controller.class);

    private Image image = new Image(Main.class.getResourceAsStream("gvim.png"));
    @FXML
    private ImageView image1;
    @FXML
    private Pane rootPane;

    public static void main(String[] args) {
        GoMethod.Instance.UploadFile("C:\\Users\\scarecrow\\Desktop\\属狗.jpg");
        ImageView imageView = new ImageView();
        imageView.setImage(new Image("./gvim.png"));
    }

    @FXML
    public void onClicked() {
        System.out.println(image1);
    }
    
    @FXML
    public void onMouseClicked() {
        FileChooser fileChooser = new FileChooser();
        fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("只能选择图片", "*.jpg"));
        File file = fileChooser.showOpenDialog(null);

        System.out.println("你选择的文件："+file.getAbsolutePath());

        String fileUrl = GoMethod.Instance.UploadFile(file.getAbsolutePath());
        if(fileUrl != null){
            System.out.println("图片上传成功，访问地址："+fileUrl);
        }
    }

    private void logEvent(MouseEvent e){

        logger.info("eventName:{},getX{}",e.getEventType().getName(),e.getX());
        logger.info("eventName:{},getY{}",e.getEventType().getName(),e.getY());

        logger.info("eventName:{},getSceneX{}",e.getEventType().getName(),e.getSceneX());
        logger.info("eventName:{},getSceneY{}",e.getEventType().getName(),e.getSceneY());

        logger.info("eventName:{},getScreenX{}",e.getEventType().getName(),e.getScreenX());
        logger.info("eventName:{},getScreenY{}",e.getEventType().getName(),e.getScreenY());
    }

    double initSceneX;
    double initSceneY;
    @FXML
    public void onDraggedWindow(MouseEvent e) {
        if (e.getEventType() == MouseEvent.MOUSE_PRESSED) {    //鼠标按下的事件
            initSceneX = e.getSceneX();
            initSceneY = e.getSceneY();
        } else if (e.getEventType() == MouseEvent.MOUSE_DRAGGED) {  //鼠标拖动的事件
            StageHelper.getStages().get(0).setX(e.getScreenX() - initSceneX);
            StageHelper.getStages().get(0).setY(e.getScreenY() - initSceneY);
        }
    }

    @FXML
    public void onDragDropped(DragEvent event) {
        Dragboard db = event.getDragboard();
        boolean success = false;
        if (db.hasFiles()) {
            System.out.println(db.getFiles().toString());
            for (File file : db.getFiles()){
                UIUtils.notify("【"+file.getName()+"】" + "上传成功", NotifyType.NOTIFY_SUCCESS);
            }
            success = true;
        }
        event.setDropCompleted(success);

        event.consume();
    }
    @FXML
    public void onDragOver(DragEvent event) {
        if (event.getDragboard().hasFiles()) {
            event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
        }
        event.consume();
    }

    public ImageView getImage1() {
        return image1;
    }

    public void setImage1(ImageView image1) {
        this.image1 = image1;
    }

    public void initialize(URL location, ResourceBundle resources) {
        image1.setImage(image);
    }
}

